//
//  LEForgetController.m
//  LoveExpressBusiness
//
//  Created by 刘甲奇 on 2017/5/9.
//  Copyright © 2017年 DFH. All rights reserved.
//

#import "LEForgetController.h"
#import "MakerTools.h"
#import "NSString+MD5.h"
#import <SMS_SDK/SMSSDK.h>

@interface LEForgetController ()
@property (weak, nonatomic) IBOutlet UITextField *phoneField;
@property (weak, nonatomic) IBOutlet UIButton *getCodeButton;
@property (weak, nonatomic) IBOutlet UITextField *codeField;
@property (weak, nonatomic) IBOutlet UITextField *passwordField;
@property (weak, nonatomic) IBOutlet UITextField *passwordField1;
@property (weak, nonatomic) IBOutlet UIButton *resetButton;

@property(nonatomic,strong)NSTimer *timer;
@property(nonatomic,assign)NSInteger second;

@end

@implementation LEForgetController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"忘记密码";
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.timer invalidate];
    self.timer = nil;
}
- (void)dealloc
{
    LELog(@"%s---%d",__func__,__LINE__);
}
#pragma mark- 网络请求
- (void)requestSavePassword
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    NSString *paramString = [NSString stringWithFormat:@"password|%@,telphone|%@",[self.passwordField.text stringToMD5],self.phoneField.text];
    [dict setObject:paramString forKey:@"params"];
    
    [SVProgressHUD showWithStatus:@"正在保存中"];
    __weak typeof (self) weakSelf = self;
    [API netAccess:LoginForgetPasswordUrl andMethor:@"POST" andDict:dict success:^(NSDictionary *successCode){
        [SVProgressHUD dismiss];
        LELog(@"%s---%d---%@",__func__,__LINE__,successCode);
        id datas = successCode[@"datas"];
        if ([datas isKindOfClass:[NSDictionary class]])
        {
            NSString *error = datas[@"error"];
            if (error)
            {
                [SVProgressHUD showErrorWithStatus:error];
            }
        }else if ([datas isKindOfClass:[NSString class]])
        {
            [SVProgressHUD showSuccessWithStatus:datas];
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }

    } falure:^(NSError *er) {
        LELog(@"%s---%d---%@",__func__,__LINE__,er);
        [SVProgressHUD dismiss];
        [SVProgressHUD showErrorWithStatus:@"网络错误"];
    }];
  
}

#pragma mark- 点击事件
//获取验证码
- (IBAction)getCodeClick:(id)sender
{
    
    if ([MakerTools isPhoneNumber:self.phoneField.text]) {
        self.getCodeButton.enabled = NO;
        [self requestCode];
    }else
    {
        [SVProgressHUD showInfoWithStatus:@"请输入正确的手机号"];
    }
}

- (IBAction)resetClick:(UIButton *)sender
{
    [self.passwordField1 resignFirstResponder];
    [self checkCode];
    
}
#pragma mark- privateMethod
/**
 初始化 timer
 */
- (void)initTimer
{
    self.second = 60;
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerCallBack) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
}
- (void)timerCallBack
{
    self.second--;
    [self.getCodeButton setTitle:[NSString stringWithFormat:@"%ld s",_second] forState:0];
    if (_second == 0) {
        self.getCodeButton.enabled = YES;
        [self.getCodeButton setTitle:@"重新获取" forState:0];
        [self.timer invalidate];
        self.timer = nil;
        return ;
    }
}

/**
 获取验证码
 */
- (void)requestCode
{
    __weak typeof (self) weakSelf = self;
    [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:self.phoneField.text zone:@"86" result:^(NSError *error) {
        if (!error) {
            [SVProgressHUD showInfoWithStatus:@"获取验证码成功"];
            [weakSelf initTimer];
        }else{
            LELog(@"%s---%d---%@",__func__,__LINE__,error);
            [SVProgressHUD showErrorWithStatus:@"获取验证码失败"];
            weakSelf.getCodeButton.enabled = YES;
        }
    }];
}

/**
 验证验证码
 */
- (void)checkCode
{
    if ([self checkEmpty]) {
        return;
    }
    __weak typeof (self) weakSelf = self;
    [SMSSDK commitVerificationCode:self.codeField.text phoneNumber:self.phoneField.text zone:@"86" result:^(NSError *error){
        if (!error)
        {
            [SVProgressHUD showErrorWithStatus:@"验证成功"];
            [weakSelf checkPassword];
        }
        else
        {
            [SVProgressHUD showErrorWithStatus:@"验证码验证错误"];
        }
    }];
}

/**
 验证是够为空等
 */
- (BOOL)checkEmpty
{
    if (![MakerTools isPhoneNumber:self.phoneField.text]) {
        [SVProgressHUD showInfoWithStatus:@"输入正确手机号"];
        return YES;
    }
    if ([self.codeField.text isEqualToString:@""]) {
        [SVProgressHUD showInfoWithStatus:@"验证码不能为空"];
        return YES;
    }
    return NO;
}

/**
 检验密码
 */
- (void)checkPassword
{
    if (self.passwordField.text.length < 6 || self.passwordField1.text.length <6)
    {
        [SVProgressHUD showInfoWithStatus:@"密码不能小于6位"];
        return;
    }
    if (![self.passwordField1.text isEqualToString:self.passwordField.text]) {
        [SVProgressHUD showInfoWithStatus:@"两次输入不相同"];
        return;
    }
    [self requestSavePassword];
}



@end







